package com.iweb.project.Category.dao;

import com.iweb.project.Category.model.Category;
import com.iweb.project.util.JdbcUtil;
import com.iweb.project.util.PageModel;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class CategoryDaoImpl implements CategoryDao {

    @Override
    public List<Category> selectByPage(PageModel pageModel) {
        List<Category> categoryList = new ArrayList<>();
        List<Map<String, Object>> list = null;
        String likeValue = pageModel.getLikeValue() == null ? "" : pageModel.getLikeValue();
        StringBuffer sql = new StringBuffer("select cid, dish_name, dish_type, dish_description, price " +
                " from dishes where cid like ? or dish_name like ? or dish_description like ?");
        //查询总条数
        int recordCount = JdbcUtil.jdbcBySelect(sql.toString(), "%" + likeValue + "%", "%" + likeValue + "%", "%" + likeValue + "%").size();
        pageModel.setRecordCount(recordCount);
        int startCount = (pageModel.getPageNo() - 1) * pageModel.getPageSize();
        //分页查询
        sql.append(" order by cid desc limit ? , ?");
        list = JdbcUtil.jdbcBySelect(sql.toString(), "%" + likeValue + "%", "%" + likeValue + "%", "%" + likeValue + "%",
                startCount, pageModel.getPageSize());
        if (list != null && list.size() > 0) {
            for (int i = 0; i < list.size(); i++) {
                Map<String, Object> map = list.get(i);
                Integer cid = (Integer) map.get("cid");
                String dish_name = (String) map.get("dish_name");
                String dish_type = (String) map.get("dish_type");
                String dish_description = (String) map.get("dish_description");
                Integer price = (Integer) map.get("price");
                Category category = new Category(cid, dish_name, dish_type, dish_description, price);
                categoryList.add(category);
            }
        }
        return categoryList;
    }

    @Override
    public int addOne(Category category) {
        String sql = "insert into dishes values(null, ?, ?, ?, ?)";
        return JdbcUtil.jdbcByUpdate(sql, category.getDish_name(), category.getDish_type(), category.getDish_description(), category.getPrice());
    }

    @Override
    public int updateOne(Category category) {
        String sql = "update dishes set dish_name=?, dish_type=?, dish_description=?, price=? " +
                "where cid=?";
        return JdbcUtil.jdbcByUpdate(sql, category.getDish_name(), category.getDish_type(), category.getDish_description(), category.getPrice(), category.getcid());
    }

    @Override
    public Category queryById(String cid) {
        String sql = "select cid, dish_name, dish_type, dish_description, price " +
                " from dishes where cid=?";
        List<Map<String, Object>> list = JdbcUtil.jdbcBySelect(sql, cid);
        Map<String, Object> map = list.get(0);

        String dish_name = (String) map.get("dish_name");
        String dish_type = (String) map.get("dish_type");
        String dish_description = (String) map.get("dish_description");
        Integer price = (Integer) map.get("price");
        Category category = new Category(Integer.valueOf(cid), dish_name, dish_type, dish_description, price);
        return category;
    }

    @Override
    public int deleteById(String cid) {
        String sql = "delete from dishes where cid=?";
        int res = JdbcUtil.jdbcByUpdate(sql, cid);
        return res;
    }
}
